﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

using System.Text;
using WinnerCMS.Common;

using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_OrderList。
    /// </summary>
    public class OrderListDal
    {

        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="CartGUID"></param>
        /// <param name="OrderId"></param>
        /// <param name="Remark"></param>
        public void Add(string CartGUID, int OrderId, string Remark)
        {
            //SqlParameter[] arrParameter = new SqlParameter[3];
            //arrParameter[0] = new SqlParameter("OrderId", SqlDbType.Int, 4);
            //arrParameter[1] = new SqlParameter("Remark", SqlDbType.NVarChar);
            //arrParameter[2] = new SqlParameter("CartGUID", SqlDbType.NVarChar, 50);

            //arrParameter[0].Value = OrderId;
            //arrParameter[1].Value = Remark;
            //arrParameter[2].Value = CartGUID;


            //{
            //    SQLBaseV2.ExecuteNonQuery("P_CMS_OrderList_Add", CommandType.StoredProcedure, arrParameter);
            //}
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="GoodsID"></param>
        /// <param name="OrderId"></param>
        /// <param name="Remark"></param>
        public void Add(int GoodsID, int OrderId, string Remark)
        {
//            SqlParameter[] arrParameter = new SqlParameter[3];

//            arrParameter[0] = new SqlParameter("OrderId", SqlDbType.Int, 4);
//            arrParameter[1] = new SqlParameter("Remark", SqlDbType.NVarChar);
//            arrParameter[2] = new SqlParameter("GoodsID", SqlDbType.Int, 4);


//            arrParameter[0].Value = OrderId;
//            arrParameter[1].Value = Remark;
//            arrParameter[2].Value = GoodsID;

             
//                SQLBaseV2.ExecuteNonQuery(@"INSERT INTO [CMS_OrderList]
//           ([ModelId]
//			,[GoodsID]
//			,[OriginalPrice]
//			,[Price]
//			,[Quantity]
//			,[SubTotal]
//			,[PresentExp]
//			,[OrderId]
//			,[Remark]
//           )
//(SELECT S.ModelId,S.Id as GoodsID,S.PriceMarket ,S.PriceMember,1 as Quantity,(S.PriceMember * 1)  as SubTotal ,S.PresentExp,@OrderId as [OrderId], @Remark as [Remark]
            //FROM CMS_Goods as S where S.Id=@GoodsID)", arrParameter);
             
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(OrderList model, string AdminName, string IP)
        {

            //SqlParameter[] arrParameter = new SqlParameter[12];

            //arrParameter[0] = new SqlParameter("OrderId", SqlDbType.Int, 4);
            //arrParameter[1] = new SqlParameter("GoodsID", SqlDbType.Int, 4);
            //arrParameter[2] = new SqlParameter("SaleType", SqlDbType.Int, 4);
            //arrParameter[3] = new SqlParameter("OriginalPrice", SqlDbType.Money, 8);
            //arrParameter[4] = new SqlParameter("Price", SqlDbType.Money, 8);
            //arrParameter[5] = new SqlParameter("Quantity", SqlDbType.Int, 4);
            //arrParameter[6] = new SqlParameter("SubTotal", SqlDbType.Money, 8);
            //arrParameter[7] = new SqlParameter("PresentExp", SqlDbType.Int, 4);
            //arrParameter[8] = new SqlParameter("Remark", SqlDbType.NVarChar, 1000);
            //arrParameter[9] = new SqlParameter("Id", SqlDbType.Int, 4);
            //arrParameter[10] = new SqlParameter("Admin", SqlDbType.NVarChar, 50);
            //arrParameter[11] = new SqlParameter("IP", SqlDbType.NVarChar, 50);


            //arrParameter[0].Value = model.OrderId;
            //arrParameter[1].Value = model.GoodsID;
            //arrParameter[2].Value = model.SaleType;
            //arrParameter[3].Value = model.OriginalPrice;
            //arrParameter[4].Value = model.Price;
            //arrParameter[5].Value = model.Quantity;
            //arrParameter[6].Value = model.SubTotal;
            //arrParameter[7].Value = model.PresentExp;
            //arrParameter[8].Value = model.Remark;
            //arrParameter[9].Value = model.Id;
            //arrParameter[10].Value = AdminName;
            //arrParameter[11].Value = IP;



            //SQLBaseV2.ExecuteNonQuery("P_CMS_OrderList_Edit", CommandType.StoredProcedure, arrParameter);

        }
        #endregion
         
        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        public OrderList GetInfo(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.OrderList.FirstOrDefault(x => x.Id == Id);
            }
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="OrderId"></param>
        /// <returns></returns>
        public IList<OrderList> BindList(int OrderId)
        {
            using (Entities db = new Entities())
            {
                return db.OrderList.Where(x => x.OrderId == OrderId).ToList();
            }
        }
        #endregion

        #region 统计
        /// <summary>
        /// 统计
       /// </summary>
       /// <param name="Type"></param>
       /// <returns></returns>
        public dynamic Rank(int Type)
        {
            /*
             SELECT CMS_Goods.ContentTitle, CMS_Goods.Unit, SUM(CMS_OrderList.Quantity) AS Quantity, 
                          SUM(CMS_OrderList.Price * CMS_OrderList.Quantity) AS Total
    FROM         CMS_OrderList INNER JOIN
                          CMS_Goods ON CMS_OrderList.GoodsID = CMS_Goods.Id
              GROUP BY CMS_Goods.ContentTitle, CMS_Goods.Unit
             */

            using (Entities db = new Entities())
            {

                var q = from x in db.Goods
                        join y in db.OrderList on x.Id equals y.GoodsId
                        group   new{x,y}  by new { x.ContentTitle, x.Unit } into g
                        select new { g.Key.ContentTitle, g.Key.Unit, Quantity = g.Sum(ol => ol.y.Quantity), Total = g.Sum(ol => ol.y.Quantity * ol.y.Price) };

                return q.ToList();
            }

        }

        #endregion
    }
}